﻿package mnsolucoes {
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.display.Stage;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	/**
import mnsolucoes.Scroll;

var sc:Scroll = new Scroll(stage);
sc.setScrollerMovieClip(mcTexto)
	.setScrollBar(mcBar)
	.setScrollTracker(mcTrack)
	.setScrollMarginLeft(15);
	
sc.init();
	*/
	
	public class Scroll extends MovieClip {
		
		protected var scrollerMovieClip:MovieClip;	//Objeto que será o alvo do scroll: MC com texto/imagem dentro
		protected var scrollTracker:MovieClip;		//tracker da barra de scrool
		protected var scrollBar:MovieClip;			//barra do scroll
		protected var _stage:Stage;					//estágio
		protected var scrollMarginLeft:Number = 15;	//margem entre a barra de scroll e o scrollerMC
		protected var arrowTop:MovieClip = null;
		protected var arrowBotton:MovieClip = null;
		protected var scrollTrackerResize:Boolean = false;
		
		private var barra:MovieClip;	//Barra = scrollTrack + scrollBar
		private var mascara:MovieClip;	
		private var areaHeight:Number;	//height total do scrollerMovieClip
		private var areaWidth:Number;	//width total barra de scroll + scrollerMovieClip
		private var minTrackY:Number;	//y mínima do tracker
		private var maxTrackY:Number;	//y maximo do tracker
		private var timer:Timer;
		private var iniciado:Boolean = false;
		
		public function Scroll(_stage:Stage) {
			barra = new MovieClip();
			mascara = new MovieClip();
			this._stage = _stage;
		}
		
		public function init():Boolean {
			if (iniciado) return false;
			
			montaBarra();
			criaMascara();
			
			if (scrollTrackerResize) {
				scrollTracker.height = (mascara.height / scrollerMovieClip.height) * scrollBar.height;
				setTrackY();
			}
			
			criaEventos();
			setTimer();
			
			iniciado = true;
			return true;
		}
		
		private function setTimer():void {
			timer = new Timer(20, 0);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent) { posiciona(); });
		}
		
		private function posiciona() {
			var percentuale:Number = Math.round((100 / maxTrackY) * scrollTracker.y);
			
			var upY:Number = 0;
			var downY:Number = scrollerMovieClip.height - (mascara.height / 2);
			 
			var fx:Number = mascara.y - (((downY - (mascara.height/2)) / 100) * percentuale); 
			
			var curry:Number = scrollerMovieClip.y; 
			var finalx:Number = fx; 
			
			if (curry != finalx) {
				var diff:Number = finalx-curry;
				curry += diff / 6; 
			}
			
			scrollerMovieClip.y = curry;			
		}
		
		private function criaEventos():void {
			scrollTracker.buttonMode = true;
			scrollTracker.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent) {
				scrollTracker.startDrag(false, new Rectangle(scrollTracker.x, minTrackY, 0, maxTrackY));									  
			});

			_stage.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent) {
				scrollTracker.stopDrag();
			});
			
			_stage.addEventListener(MouseEvent.MOUSE_WHEEL, function(e:MouseEvent) {
				var newY:Number = scrollTracker.y;
				var _delta:Number;
				
				newY = newY-e.delta;
				
				if (newY<minTrackY) newY = minTrackY;
				if (newY>maxTrackY) newY = maxTrackY;
				
				scrollTracker.y = newY;
			});			
			
			scrollBar.buttonMode = true;
			scrollBar.addEventListener(MouseEvent.CLICK, onmouseclick);
			
			if (arrowTop!=null) {
				arrowTop.buttonMode = true;
				arrowTop.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
					var newY:Number = scrollTracker.y;
					newY = newY-(scrollBar.height*0.1);
					if (newY<minTrackY) newY = minTrackY;
					scrollTracker.y = newY;
				});
			} 
			if (arrowBotton!=null) {
				arrowBotton.buttonMode = true;
				arrowBotton.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
					var newY:Number = scrollTracker.y;
					newY = newY+(scrollBar.height*0.1);
					if (newY>maxTrackY) newY = maxTrackY;
					scrollTracker.y = newY;
				});
			} 
		}
		
		private function onmouseclick(e:MouseEvent):void {
			var newY:Number = scrollTracker.y;
			var posicaoReal:Number = scrollTracker.y+Math.round(scrollTracker.height/2);
				
			if (e.localY>posicaoReal) newY = newY+(scrollBar.height*0.1);
			else newY = newY-(scrollBar.height*0.1);
				
			if (newY<minTrackY) newY = minTrackY;
			if (newY>maxTrackY) newY = maxTrackY;
				
			scrollTracker.y = newY;
		}
		
		private function criaMascara():void {
			mascara.graphics.beginFill(0xFF0000);
			mascara.graphics.drawRect(0, 0, scrollerMovieClip.width+20, areaHeight+20);
			mascara.graphics.endFill();
			
			scrollerMovieClip.parent.addChild(mascara);
			mascara.x = scrollerMovieClip.x;
			mascara.y = scrollerMovieClip.y;
			
			scrollerMovieClip.mask = mascara;
		}		
		
		private function montaBarra():void {
			barra.addChild(scrollBar);
			barra.addChild(scrollTracker);
			if (arrowTop!=null) barra.addChild(arrowTop);
			if (arrowBotton!=null) barra.addChild(arrowBotton);

			centralizaObjetosDaBarra();
			scrollerMovieClip.parent.addChild(barra);
			barra.x = scrollerMovieClip.x+scrollerMovieClip.width+scrollMarginLeft;			
			barra.y = scrollerMovieClip.y+25//eu;
			
		}
		
		private function centralizaObjetosDaBarra():void {
			setTrackY();
			
			scrollTracker.x = 0;
			scrollTracker.y = minTrackY;
			scrollBar.x = 0;
			scrollBar.y = 0;
			if (arrowTop!=null) {
				arrowTop.x=0;
				arrowTop.y = minTrackY-arrowTop.height;
			}
			if (arrowBotton!=null) {
				arrowBotton.x=0;
				arrowBotton.y = maxTrackY + Math.round(arrowBotton.height);
			}
			
			centralizaObjecto(scrollBar);
			centralizaObjecto(scrollTracker);
			if (arrowTop!=null) centralizaObjecto(arrowTop);
			if (arrowBotton!=null) centralizaObjecto(arrowBotton);
		}		
		
		private function centralizaObjecto(obj:MovieClip):void {
			obj.x = Math.round((barra.width-obj.width)/2);
		}		
		
		private function setTrackY() {
			minTrackY = 0;
			maxTrackY = scrollBar.height-scrollTracker.height;
		}		
		
		
		/**
		* Getter e Setters
		*/
		
		public function getScrollTrackerResize():Boolean {
			return this.scrollTrackerResize;
		}
		
		public function setScrollTrackerResize(scrollTrackerResize:Boolean):Object {
			this.scrollTrackerResize = scrollTrackerResize;
			return this;
		}
		
		public function getArrowBotton():MovieClip {
			return this.arrowBotton;
		}
		
		public function setArrowBotton(arrowBotton):Object {
			this.arrowBotton = arrowBotton;
			return this;
		}
		
		public function getArrowTop():MovieClip {
			return this.arrowTop;
		}
		
		public function setArrowTop(arrowTop:MovieClip):Object {
			this.arrowTop = arrowTop;
			return this;
		}
		
		public function getScrollMarginLeft():Number {
			return this.scrollMarginLeft;
		}
		
		public function setScrollMarginLeft(scrollMarginLeft:Number):Object{
			this.scrollMarginLeft = scrollMarginLeft;
			return this;
		}		
		
		public function getScrollBar():MovieClip {
			return this.scrollBar;
		}
		
		public function setScrollBar(scrollBar:MovieClip): Object {
			this.scrollBar = scrollBar;
			areaHeight = scrollBar.height;
			return this;
		}
		
		public function getScrollTracker():MovieClip {
			return this.scrollTracker;
		}
		
		public function setScrollTracker(scrollTracker:MovieClip):Object {
			this.scrollTracker = scrollTracker;
			return this;
		}
		
		public function getScrollerMovieClip():MovieClip {
			return this.scrollerMovieClip;
		}
		
		public function setScrollerMovieClip(scrollerMovieClip:MovieClip):Object {
			this.scrollerMovieClip = scrollerMovieClip;
			areaWidth = scrollerMovieClip.width;
			return this;
		}
		
		
	}
	
}
